package com.danskin.wechat.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.danskin.wechat.dao.CustGiftRecordDao;
import com.danskin.wechat.entity.CustGiftRecord;
import com.danskin.wechat.service.CustGiftRecordService;
import com.danskin.wechat.service.SysAreaService;
import com.danskin.wechat.util.DateUtils;
import com.danskin.wechat.util.LogUtil;
import com.danskin.wechat.util.Page;
import com.danskin.wechat.vo.CustGiftRecordVo;

@Service
public class CustGiftRecordServiceImpl implements CustGiftRecordService {

	@Autowired
	private CustGiftRecordDao custGiftRecordDao;

	@Autowired
	private SysAreaService areaService;

	public List<CustGiftRecordVo> listPageCustGiftRecord(Page page) {
		LogUtil.info("=======分页查询奖品领取记录参数：" + ToStringBuilder.reflectionToString(page));

		List<CustGiftRecordVo> custGiftRecordList = new ArrayList<CustGiftRecordVo>();
		Map<String, Object> map = page.getMap();
		String newStartTime = (String) map.get("startTime");
		String newTime = (String) map.get("endTime");
		if (StringUtils.isNotEmpty(newStartTime)) {
			map.put("newStartTime", newStartTime + " " + "00:00:01");
		}
		if (StringUtils.isNotEmpty(newTime)) {
			map.put("newTime", newTime + " " + "23:59:59");
		}
		try {
			custGiftRecordList = custGiftRecordDao.listPageCustGiftRecord(page);

			// 根据list中的省市区code获取省市区名称
			custGiftRecordList = getProvCityAndDistrict(custGiftRecordList);
		} catch (Exception e) {
			LogUtil.error("=======分页查询奖品领取记录异常", e);
		}
		return custGiftRecordList;
	}

	@Override
	public CustGiftRecord selectCustGiftRecordById(Integer id) {
		LogUtil.info("=========根据主键查询用户领取实物记录参数：id=" + id);

		CustGiftRecord custGiftRecord = new CustGiftRecord();

		try {
			custGiftRecord = custGiftRecordDao.selectCustGiftRecordById(id);
		} catch (Exception e) {
			LogUtil.error("========根据主键查询用户领取实物记录异常", e);
		}
		return custGiftRecord;
	}

	@Override
	public List<CustGiftRecord> selectCustGiftRecordList(CustGiftRecord custGiftRecord) {
		LogUtil.info("=======根据查询条件查询用户实物记录数据参数：" + ToStringBuilder.reflectionToString(custGiftRecord));

		List<CustGiftRecord> goodsRecordList = new ArrayList<CustGiftRecord>();

		try {
			goodsRecordList = custGiftRecordDao.selectCustGiftRecordList(custGiftRecord);
		} catch (Exception e) {
			LogUtil.error("========根据查询条件查询用户实物记录数据异常", e);
		}
		return goodsRecordList;
	}

	@Override
	public Integer insertCustGiftRecord(CustGiftRecord custGiftRecord) {
		LogUtil.info("========新建用户实物记录参数：" + ToStringBuilder.reflectionToString(custGiftRecord));

		Integer id = 0;

		try {
			custGiftRecordDao.insertCustGiftRecord(custGiftRecord);
			id = custGiftRecord.getId();
		} catch (Exception e) {
			LogUtil.error("=======新建用户实物记录异常", e);
		}
		return id;
	}

	@Override
	public int updateCustGiftRecord(CustGiftRecord custGiftRecord) {
		LogUtil.info("========更新用户实物记录参数：" + ToStringBuilder.reflectionToString(custGiftRecord));

		int updateCount = 0;

		try {
			updateCount = custGiftRecordDao.updateCustGiftRecord(custGiftRecord);
		} catch (Exception e) {
			LogUtil.error("=======更新用户实物记录异常", e);
		}
		return updateCount;
	}

	/**
	 * 根据list中的省市区code获取省市区名称
	 * 
	 * @param storeQrcodeScanList
	 * @return
	 * @author makunsmac
	 */
	private List<CustGiftRecordVo> getProvCityAndDistrict(List<CustGiftRecordVo> custGiftRecordList) {
		if (CollectionUtils.isNotEmpty(custGiftRecordList)) {
			for (CustGiftRecordVo temp : custGiftRecordList) {
				// 根据省市区code查询对应的省市区名称
				String p = areaService.getSysAreaByCode(temp.getProvince());
				temp.setProvince(p);
				String c = areaService.getSysAreaByCode(temp.getCity());
				temp.setCity(c);
				String d = areaService.getSysAreaByCode(temp.getDistrict());
				temp.setDistrict(d);
			}
		}
		return custGiftRecordList;
	}

	@Override
	public List<CustGiftRecordVo> getCustGiftRecordExport(String mobile, String startTime, String endTime, String mpId) {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(startTime)) {
			startTime = startTime + " " + "00:00:01";
		}else{
			startTime = DateUtils.getStartTime() + " " + "00:00:01";
		}
		if (StringUtils.isNotEmpty(endTime)) {
			endTime = endTime + " " + "23:59:59";
		}else{
			endTime = DateUtils.getEndTime() + " " + "23:59:59";
		}
		return custGiftRecordDao.getCustGiftRecordExport(mobile,startTime,endTime,mpId);
	}

}
